<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>layout 管理界面大布局示例 - Layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="./layui-v2.11.2/layui/css/layui.css" rel="stylesheet">
    <link rel="stylesheet" href="./商品管理.css">
    <script src="./package/dist/echarts.js"></script>
    <style>
        .hide {
            display: none;
        }
    </style>
</head>

<body>
    <div class="layui-layout layui-layout-admin">
        <div class="layui-header">
            <div class="layui-logo layui-hide-xs layui-bg-black">商品管理后台</div>
            <!-- 头部区域（可配合layui 已有的水平导航） -->
            <ul class="layui-nav layui-layout-right">
                <li class="layui-nav-item layui-hide layui-show-sm-inline-block">
                    <a id="user_image" href="javascript:;">
                        <img src="../作业/images/年2.jpg" class="layui-nav-img">
                    </a>
                    <dl class="layui-nav-child">
                        <dd id="personal_center"><a href="javascript:;">个人中心</a></dd>
                        <!-- <dd><a href="javascript:;">Settings</a></dd> -->
                        <dd id="exit"><a href="javascript:;">退出登录</a></dd>
                    </dl>
                </li>
                <li class="layui-nav-item" lay-header-event="menuRight" lay-unselect>
                    <a href="javascript:;">
                        <i class="layui-icon layui-icon-more-vertical"></i>
                    </a>
                </li>
            </ul>
        </div>
        <div class="layui-side layui-bg-black">
            <div class="layui-side-scroll">
                <!-- 左侧导航区域（可配合layui已有的垂直导航） -->
                <ul class="layui-nav layui-nav-tree" lay-filter="test">
                    <li class="layui-nav-item layui-nav-itemed goods goods_jump_manage">
                        <a class="" href="javascript:;">商品管理</a>
                        <dl class="layui-nav-child">
                            <dd class="goods_look" onclick="dian()"><a href="javascript:;">查看全部</a></dd>
                            <dd class="goods_add" onclick="dian()"><a href="javascript:;">添加商品</a></dd>
                            <!-- <dd class="goods_del"><a href="javascript:;">删除商品</a></dd> -->
                            <dd class="goods_updata" onclick="dian()"><a href="javascript:;">更新商品</a></dd>
                        </dl>
                    </li>
                    <li class="layui-nav-item goods_jump_chart">
                        <a href="javascript:;">商品图表</a>
                        <dl class="layui-nav-child">
                            <dd><a href="javascript:;">价格表</a></dd>
                        </dl>
                    </li>
                    <!-- <li class="layui-nav-item"><a href="javascript:;">click menu item</a></li>
                    <li class="layui-nav-item"><a href="javascript:;">the links</a></li> -->
                </ul>
            </div>
        </div>
        <div class="layui-body">
            <!-- 内容主体区域 -->
            <div style="padding: 15px;">
                <blockquote class="layui-elem-quote layui-text">
                    <span>商品管理</span>
                </blockquote>
                <div class="layui-card layui-panel">
                    <div class="layui-card-header goods_title">
                        <!-- 图表 -->
                    </div>
                    <div class="layui-card-body goods_body">

                        <!-- 为 ECharts 准备一个定义了宽高的 DOM -->
                        <div id="main" style="width: 1500px;height:800px;"></div>

                    </div>
                </div>
                <br><br>
            </div>
            <!-- 详情 -->
            <div class="g_details hide">

            </div>
        </div>
        <div class="layui-footer">
            <!-- 分页 -->
            <div id="demo-laypage-normal-2" class="page"></div>

        </div>
    </div>
    <script src="./jquery.3.7.1.js"></script>

    <script src="./layui-v2.11.2/layui/layui.js"></script>
    <script>
        //当前路径
        let aa = document.location.pathname
        //图表路径
        let local_herf = `/F:/VS/%E4%B8%89%E6%AE%B5/%E9%A1%B9%E7%9B%AE-%E5%95%86%E5%93%81%E7%AE%A1%E7%90%86/%E5%95%86%E5%93%81%E5%9B%BE%E8%A1%A8.html`
        function dian() {
            if (aa == local_herf) {
                location.assign('./商品管理.html')
            } else {
                event.preventDefault()
            }
        }
        let user_name = JSON.parse(localStorage.getItem('userData')).userAccount;//用户名
        let user_pass = JSON.parse(localStorage.getItem('userData')).userCypher;//密码
        let user_image = document.querySelector('#user_image')//头像父级
        let old_img = document.querySelector('#user_image img')//默认头像
        window.onload = function () {
            let img_ = document.createElement('img');//创建头像img
            img_.src = JSON.parse(localStorage.getItem('userImg'))
            img_.classList.add('layui-nav-img')
            user_image.removeChild(old_img)
            user_image.prepend(img_)
        }

        // 用户名
        document.querySelector(`#user_image`).append(user_name)
        // 个人中心
        document.querySelector(`#personal_center`).addEventListener('click', () => {
            location.assign(`./个人中心.html`)
        })
        // 退出登录
        document.querySelector(`#exit`).addEventListener('click', () => {
            let is = confirm('确定退出登录吗?')
            if (is) {
                location.replace('./登录.html')
            }
        })



    </script>
    <script type="text/javascript">
        // 基于准备好的dom，初始化echarts实例
        var myChart = echarts.init(document.getElementById('main'));
        let goods_name_data = []//名称
        let goods_price_data = []//价格
        let goods_sold_data = []//已售
        let goods_stock_data = []//库存

        var option;
        var app = {};
        var chartDom = document.getElementById('main');
        var myChart = echarts.init(chartDom);
        // 获取数据
        $.ajax({
            type: `GET`,
            url: `https://liu.zzgoodqc.cn/goodsx/goodslist`,
            dataType: 'json',
            success: (a) => {
                var goods_data = a.data
                console.log(goods_data);
                goods_data.forEach(item => {
                    goods_name_data.push(item.goods_name)
                    goods_price_data.push(item.price)
                    goods_sold_data.push(item.sold)
                    goods_stock_data.push(item.stock)
                })

                // 指定图表的配置项和数据
                const posList = [
                    'left',
                    'right',
                    'top',
                    'bottom',
                    'inside',
                    'insideTop',
                    'insideLeft',
                    'insideRight',
                    'insideBottom',
                    'insideTopLeft',
                    'insideTopRight',
                    'insideBottomLeft',
                    'insideBottomRight'
                ];
                app.configParameters = {
                    rotate: {
                        min: -90,
                        max: 90
                    },
                    align: {
                        options: {
                            left: 'left',
                            center: 'center',
                            right: 'right'
                        }
                    },
                    verticalAlign: {
                        options: {
                            top: 'top',
                            middle: 'middle',
                            bottom: 'bottom'
                        }
                    },
                    position: {
                        options: posList.reduce(function (map, pos) {
                            map[pos] = pos;
                            return map;
                        }, {})
                    },
                    distance: {
                        min: 0,
                        max: 100
                    }
                };
                app.config = {
                    rotate: 90,
                    align: 'left',
                    verticalAlign: 'middle',
                    position: 'insideBottom',
                    distance: 15,
                    onChange: function () {
                        const labelOption = {
                            rotate: app.config.rotate,
                            align: app.config.align,
                            verticalAlign: app.config.verticalAlign,
                            position: app.config.position,
                            distance: app.config.distance
                        };
                        myChart.setOption({
                            series: [
                                {
                                    label: labelOption
                                },
                                {
                                    label: labelOption
                                },
                                {
                                    label: labelOption
                                },
                                {
                                    label: labelOption
                                }
                            ]
                        });
                    }
                };
                const labelOption = {
                    show: true,
                    position: app.config.position,
                    distance: app.config.distance,
                    align: app.config.align,
                    verticalAlign: app.config.verticalAlign,
                    rotate: app.config.rotate,
                    formatter: '{c}  {name|{a}}',
                    fontSize: 16,
                    rich: {
                        name: {}
                    }
                };
                option = {
                    title: {
                        text: '商品数据图',
                        textStyle: {
                            fontSize: 27
                        }
                    },
                    tooltip: {
                        trigger: 'axis',
                        axisPointer: {
                            type: 'shadow'
                        }
                    },
                    legend: {
                        data: ['价格', '已售', '库存']
                    },
                    toolbox: {
                        show: true,
                        orient: 'vertical',
                        left: 'right',
                        top: 'center',
                        feature: {
                            mark: { show: true },
                            dataView: { show: true, readOnly: false },
                            magicType: { show: true, type: ['line', 'bar', 'stack'] },
                            restore: { show: true },
                            saveAsImage: { show: true }
                        }
                    },
                    xAxis: [
                        {
                            type: 'category',
                            axisTick: { show: false },
                            // data: ['2012', '2013', '2014', '2015', '2016']
                            data: goods_name_data
                        }
                    ],
                    yAxis: [
                        {
                            type: 'value'
                        }
                    ],
                    series: [
                        {
                            name: '价格',
                            type: 'bar',
                            barGap: 0,
                            label: labelOption,
                            emphasis: {
                                focus: 'series'
                            },
                            data: goods_price_data
                        },
                        {
                            name: '已售',
                            type: 'bar',
                            label: labelOption,
                            emphasis: {
                                focus: 'series'
                            },
                            data: goods_sold_data
                        },
                        {
                            name: '库存',
                            type: 'bar',
                            label: labelOption,
                            emphasis: {
                                focus: 'series'
                            },
                            data: goods_stock_data
                        },

                    ]
                };
                option && myChart.setOption(option);
            },
            error: (e) => {
                alert(`请求失败`)
            }
        })
        console.log(goods_name_data);

    </script>
</body>

</html>